﻿namespace DataDuctus.MongoDb.Embedding.Server.Internal
{
	using System;
	using System.IO;
	using System.Linq;

	using SharpCompress.Archive;
	using SharpCompress.Common;

  internal static class ServerExtractor
	{
		#region Methods

    internal static string ExtractServer(string toBinPath)
		{
      string arch = Environment.Is64BitOperatingSystem ? "x64" : "x86";

      using (
        Stream strm =
          typeof(ServerExtractor).Assembly.GetManifestResourceStream(
            string.Format("DataDuctus.MongoDb.Embedding.Server.Internal.{0}.mongod.7z", arch)))
      {
        using (var archive = ArchiveFactory.Open(strm))
        {
          foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory))
          {
            entry.WriteToDirectory(toBinPath, ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite);
          }
        }
      }

      return Path.Combine(toBinPath, "mongod.exe");
    }
		#endregion
	}
}